//
//  PMSplitCtrl.cpp
//
//  Copyright (C) 2008 Visual.Wei
//  Author: Wei Shuai <cpuwolf@sina.com>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//
#include "PMSplitCtrl.h"
#include "PMListView.h"
#include "PMTabCtrl.h"

extern PMListView *p_pmlv;
extern PMTabCtrl * p_pmtc;

PMSplitCtrl::PMSplitCtrl(HWND hWnd,DWORD height):PMControl()
{
	RECT rect;
	GetClientRect(hWnd,&rect);
	m_height=height;
	g_hwnd=hWnd;
	m_bmousedown=FALSE;
	CopyRect(&WndRect,&rect);
	WndRect.top=(rect.bottom-rect.top)/2;
	WndRect.bottom=WndRect.top+(m_height/2);
	WndRect.top=WndRect.top-(m_height/2);

	Update();
}

DWORD PMSplitCtrl::ReSize()
{
	//RECT rect;
	//GetClientRect(g_hwnd,&rect);
	//CopyRect(&WndRect,&rect);
	//WndRect.top=(rect.bottom-rect.top)/2;
	//WndRect.bottom=WndRect.top+(m_height/2);
	//WndRect.top=WndRect.top-(m_height/2);

	return TRUE;
}
void PMSplitCtrl::MouseDown(DWORD fkey,DWORD x,DWORD y)
{
	POINT pt; 
	HDC hdc;
	RECT rect;


	if((y>=WndRect.top)&&(y<=WndRect.bottom))
	{
		m_bmousedown=TRUE;
		GetClientRect(g_hwnd,&rect);
		pt.x=rect.left;
		pt.y=rect.top;
		ClientToScreen( g_hwnd, &pt );
		//click in the window
		hdc=GetDC(g_hwnd);
		PatBlt(hdc,WndRect.left,WndRect.top,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top,DSTINVERT);
		//FillRgn(hdc,m_region,GetSysColorBrush(COLOR_BACKGROUND));
		ReleaseDC(g_hwnd,hdc);
		clickoffset=(y-WndRect.top);
		org_y=pt.y;
		
		InflateRect(&rect,0,-50);
		ClipCursor(&rect);
		SetCapture(g_hwnd);
	}
	else
		m_bmousedown=FALSE;


}
void PMSplitCtrl::MouseMove(DWORD fkey,DWORD x,DWORD y)
{
	//POINT pt; 
	HDC hdc;
	RECT rect;
	if(m_bmousedown)
	{
		//pt.x=x;
		//pt.y=y;
		hdc=GetDC(g_hpwnd);
		//ClientToScreen( g_hwnd, &pt );
		//SetPixel(hdc,x,y+clickoffset,RGB(255,0,0));
		PatBlt(hdc,WndRect.left,y-clickoffset+org_y,WndRect.right-WndRect.left,WndRect.bottom-WndRect.top,DSTINVERT);

		ReleaseDC(g_hwnd,hdc);
	}
}

void PMSplitCtrl::MouseUp(DWORD fkey,DWORD x,DWORD y)
{
	RECT rect;
	ReleaseCapture();
	OffsetRect(&WndRect,0,(y-(clickoffset+WndRect.top)));
	GetWindowRect(g_hwnd,&rect);
	MoveWindow(g_hwnd,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,TRUE);
	m_bmousedown=FALSE;
	//p_pmlv->ReSize();
	//p_pmtc->ReSize();

}